package com.mobile.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

/**
 * @ClassName : DateUtil.java
 * @Decription : 日期处理
 * @Version : 2018/01/11 hlh 新建作成 1.0
 */
public class DateUtil {

    private final static SimpleDateFormat sdfYear = new SimpleDateFormat("yyyy");
    private final static SimpleDateFormat sdfDay = new SimpleDateFormat("yyyy/MM/dd");
    private final static SimpleDateFormat sdfDays = new SimpleDateFormat("yyyyMMdd");
    private final static SimpleDateFormat sdfTime = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    private final static SimpleDateFormat sdfTimeForWeb = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private final static SimpleDateFormat sdfTimes = new SimpleDateFormat("yyyyMMddHHmmss");
    private final static SimpleDateFormat sdfTime2 = new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒");
    private final static SimpleDateFormat sdfDays2 = new SimpleDateFormat("yyyy-MM-dd");
    private final static SimpleDateFormat sdfTimes3 = new SimpleDateFormat("yyyyMMddHHmmssSSS");

    /**
     * 获取yyyy/MM/dd格式
     *
     * @param date
     * @return
     */
    public static String getSdfDay(Date date) {
        return sdfDay.format(date);
    }

    /**
     * 获取yyyyMMddHHmmss格式
     *
     * @return
     */
    public static String getSdfTimes() {
        return sdfTimes.format(new Date());
    }

    /**
     * 获取yyyyMMddHHmmss格式
     *
     * @return
     */
    public static String getSdfTimes3() {
        return sdfTimes3.format(new Date());
    }

    /**
     * 获取yyyyMMddHHmmss格式
     *
     * @return
     */
    public static String getSdfTimesForWeb() {
        return sdfTimeForWeb.format(new Date());
    }

    /**
     * 获取YYYY格式
     *
     * @return
     */
    public static String getYear() {
        return sdfYear.format(new Date());
    }

    /**
     * 获取YYYY/MM/DD格式
     *
     * @return
     */
    public static String getDay() {
        return sdfDay.format(new Date());
    }

    /**
     * 获取yyyyMMdd格式
     *
     * @return
     */
    public static String getDays() {
        return sdfDays.format(new Date());
    }

    /**
     * 获取yyyy/MM/dd HH:mm:ss格式
     *
     * @return
     */
    public static String getTime() {
        return sdfTime.format(new Date());
    }

    /**
     * @param s
     * @param e
     * @return boolean
     * @throws
     * @Title: compareDate
     * @Description: 日期比较，如果s>=e 返回true 否则返回false
     */
    public static boolean compareDate(String s, String e) {
        if (fomatDate(s) == null || fomatDate(e) == null) {
            return false;
        }
        return fomatDate(s).getTime() >= fomatDate(e).getTime();
    }

    /**
     * @param s
     * @param e
     * @return int
     * @throws
     * @Title: compareDateTime
     * @Description: (日期比较 ， 如果s > e 返回1 ; s = e 返回0 ; s < e 返回 - 1)
     */
    public static int compareDateTime(String s, String e) {
        if (fomatDate(s) == null) {
            return -1;
        } else if (fomatDate(e) == null) {
            return 1;
        } else if (str2Date(s).getTime() > str2Date(e).getTime()) {
            return 1;
        } else if (str2Date(s).getTime() == str2Date(e).getTime()) {
            return 0;
        } else {
            return -1;
        }
    }

    /**
     * 字符串转日期格式（yyyy/MM/dd）
     *
     * @param date 字符串类型正确日期数据
     * @return 日期
     */
    public static Date fomatDate(String date) {
        DateFormat fmt = new SimpleDateFormat("yyyy/MM/dd");
        try {
            return fmt.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 字符串转日期格式（yyyy/MM/dd hh24:mi:ss"）
     *
     * @param date 字符串类型正确日期数据
     * @return 日期
     */
    public static Date fomatDateTime(String date) {
        DateFormat fmt = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        try {
            return fmt.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 校验日期是否合法
     *
     * @return
     */
    public static boolean isValidDate(String s) {
        DateFormat fmt = new SimpleDateFormat("yyyy/MM/dd");
        try {
            fmt.parse(s);
            return true;
        } catch (Exception e) {
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            return false;
        }
    }

    /**
     * 获取日期间隔年
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static int getDiffYear(String startTime, String endTime) {
        DateFormat fmt = new SimpleDateFormat("yyyy/MM/dd");
        try {
            // long aa=0;
            int years = (int) (((fmt.parse(endTime).getTime() - fmt.parse(
                    startTime).getTime()) / (1000 * 60 * 60 * 24)) / 365);
            return years;
        } catch (Exception e) {
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            return 0;
        }
    }

    /**
     * <li>功能描述：时间相减得到天数
     *
     * @param beginDateStr
     * @param endDateStr
     * @return long
     * @author Administrator
     */
    public static long getDaySub(String beginDateStr, String endDateStr) {
        long day = 0;
        SimpleDateFormat format = new SimpleDateFormat(
                "yyyy/MM/dd");
        Date beginDate = null;
        Date endDate = null;

        try {
            beginDate = format.parse(beginDateStr);
            endDate = format.parse(endDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
        // System.out.println("相隔的天数="+day);

        return day;
    }

    /**
     * <li>功能描述：时间相减得到小时数
     *
     * @param beginDateStr (毫秒为单位)
     * @param endDateStr   (毫秒为单位)
     * @return long
     * @author Administrator
     */
    public static long getHourSub(long beginDate, long endDate) {
        long hour = 0;
        hour = (endDate - beginDate) / (60 * 60 * 1000);
        return hour;
    }

    /**
     * 得到n天之后的日期
     *
     * @param days
     * @return
     */
    public static String getAfterDayDate(String date, String days) {
        int daysInt = Integer.parseInt(days);

        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.setTime(fomatDate(date));
        canlendar.add(Calendar.DATE, daysInt); // 日期加 如果不够减会将月变动
        Date afterDate = canlendar.getTime();

        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy/MM/dd");
        String dateStr = sdfd.format(afterDate);

        return dateStr;
    }

    /**
     * 得到n天之后的日期
     *
     * @param days
     * @return
     */
    public static String getAfterDayDate2(String date, String days) {
        int daysInt = Integer.parseInt(days);

        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.setTime(fomatDate(date));
        canlendar.add(Calendar.DATE, daysInt); // 日期加 如果不够减会将月变动
        Date afterDate = canlendar.getTime();

        String dateStr = sdfDays2.format(afterDate);

        return dateStr;
    }

    /**
     * 得到n天之后的日期（yyyy/MM/dd hh24:mi:ss）
     *
     * @param days
     * @return
     */
    public static String getPreDayDateTime(String date, int days) {
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.setTime(fomatDateTime(date));
        canlendar.add(Calendar.DATE, days); // 日期减 如果不够减会将月变动
        Date preDate = canlendar.getTime();

        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String dateStr = sdfd.format(preDate);

        return dateStr;
    }

    /**
     * 得到n天之前的日期（yyyy/MM/dd hh24:mi:ss）
     *
     * @param days
     * @return
     */
    public static String getBeforeDays(int days) {
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.setTime(new Date());
        canlendar.add(Calendar.DATE, -days); // 日期减
        Date preDate = canlendar.getTime();

        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdfd.format(preDate);

        return dateStr;
    }

    /**
     * 获取昨天的零点开始时间（yyyy/MM/dd hh24:mi:ss）
     *
     * @param days
     * @return
     */
    public static String getYesterdayBeginTime() {
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.setTime(fomatDateTime(getDay() + " 00:00:00"));
        canlendar.add(Calendar.DATE, -1); // 日期减 如果不够减会将月变动
        Date preDate = canlendar.getTime();

        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String dateStr = sdfd.format(preDate);

        return dateStr;
    }

    /**
     * 获取昨天的结束时间（yyyy/MM/dd hh24:mi:ss）
     *
     * @param days
     * @return
     */
    public static String getYesterdayEndTime() {
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.setTime(fomatDateTime(getDay() + " 23:59:59"));
        canlendar.add(Calendar.DATE, -1); // 日期减 如果不够减会将月变动
        Date preDate = canlendar.getTime();

        SimpleDateFormat sdfd = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        String dateStr = sdfd.format(preDate);

        return dateStr;
    }

    /**
     * 得到n天之后是周几
     *
     * @param days
     * @return
     */
    public static String getAfterDayWeek(String days) {
        int daysInt = Integer.parseInt(days);
        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.add(Calendar.DATE, daysInt); // 日期减 如果不够减会将月变动
        Date date = canlendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("E");
        String dateStr = sdf.format(date);
        return dateStr;
    }

    /**
     * 得到本周开始时间
     *
     * @return
     */
    public static Date getStartDateWeek() {

        Date date = new Date();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayofweek == 1) {
            dayofweek += 7;
        }
        calendar.add(Calendar.DATE, 2 - dayofweek);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        return calendar.getTime();

    }

    /**
     * 得到本周结束时间
     *
     * @return
     */
    public static Date getEndDateWeek() {
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(getStartDateWeek());
        calendar.add(Calendar.DAY_OF_WEEK, 6);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 得到上周开始时间
     *
     * @return
     */
    public static Date getStartDateFrontWeek() {
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(getStartDateWeek());
        calendar.add(Calendar.DAY_OF_MONTH, -7);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 得到上周结束时间
     *
     * @return
     */
    public static Date getEndDateFrontWeek() {
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(getEndDateWeek());
        calendar.add(Calendar.DAY_OF_MONTH, -7);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 得到明天开始时间
     *
     * @return
     */
    public static Date getStartDateNextDays() {
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 得到明天结束时间
     *
     * @return
     */
    public static Date getEndDateNextDays() {
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 得到下周开始时间
     *
     * @return
     */
    public static Date getStartDateNextWeek() {
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(getStartDateWeek());
        calendar.add(Calendar.DAY_OF_MONTH, 7);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 得到下周结束时间
     *
     * @return
     */
    public static Date getEndDateNextWeek() {
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(getEndDateWeek());
        calendar.add(Calendar.DAY_OF_MONTH, 7);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 本月开始时间
     *
     * @return
     */
    public static Date getStartMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 本月结束时间
     *
     * @return
     */
    public static Date getEndMonth() {
        Calendar calendar = Calendar.getInstance();
        int lastData = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DATE, lastData);
        calendar.set(Calendar.HOUR, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();

    }

    /**

     获取当月的结束时间戳
     */

    public static Date getMonthEndTime() {
        Long currentTime = System.currentTimeMillis();

        String timeZone = "GMT+8:00";

        Calendar calendar = Calendar.getInstance();// 获取当前日期

        calendar.setTimeZone(TimeZone.getTimeZone(timeZone));

        calendar.setTimeInMillis(currentTime);

        calendar.add(Calendar.YEAR, 0);

        calendar.add(Calendar.MONTH, 0);

        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));// 获取当前月最后一天

        calendar.set(Calendar.HOUR_OF_DAY, 23);

        calendar.set(Calendar.MINUTE, 59);

        calendar.set(Calendar.SECOND, 59);

        calendar.set(Calendar.MILLISECOND, 999);

        return calendar.getTime();

    }

    /**
     * 上月开始时间
     *
     * @return
     */
    public static Date getStartFrontMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 上月结束时间
     *
     * @return
     */
    public static Date getEndFrontMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        int lastData = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DATE, lastData);
        calendar.set(Calendar.HOUR, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 下月开始时间
     *
     * @return
     */
    public static Date getStartNextMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DATE, 1);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 下月结束时间
     *
     * @return
     */
    public static Date getEndNextMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, 1);
        int lastData = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DATE, lastData);
        calendar.set(Calendar.HOUR, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime();
    }

    /**
     * 按照yyyy/MM/dd HH:mm:ss的格式，日期转字符串
     *
     * @param date
     * @return yyyy/MM/dd HH:mm:ss
     */
    public static String date2Str(Date date) {
        return date2Str(date, "yyyy/MM/dd HH:mm:ss");
    }

    /**
     * 按照yyyy/MM/dd HH:mm:ss的格式，字符串转日期
     *
     * @param date
     * @return
     */
    public static Date str2Date(String date) {
        if (StringUtil.notEmpty(date)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            try {
                return sdf.parse(date);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return new Date();
        } else {
            return null;
        }
    }

    /**
     * 按照参数format的格式，日期转字符串
     *
     * @param date
     * @param format
     * @return
     */
    public static String date2Str(Date date, String format) {
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        } else {
            return "";
        }
    }

    /**
     * 按照参数format的格式，日期转字符串(yyyy/MM/dd HH:mm:ss)
     *
     * @param date
     * @param format
     * @return
     */
    public static String date2StrBySdfTime(Date date) {
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            return sdf.format(date);
        } else {
            return "";
        }
    }

    /**
     * 把时间根据时、分、秒转换为时间段
     *
     * @param StrDate
     */
    public static String getTimes(String StrDate) {
        String resultTimes = "";

        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date now;

        try {
            now = new Date();
            Date date = df.parse(StrDate);
            long times = now.getTime() - date.getTime();
            long day = times / (24 * 60 * 60 * 1000);
            long hour = (times / (60 * 60 * 1000) - day * 24);
            long min = ((times / (60 * 1000)) - day * 24 * 60 - hour * 60);
            long sec = (times / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);

            StringBuffer sb = new StringBuffer();
            //sb.append("发表于：");
            if (hour > 0) {
                sb.append(hour + "小时前");
            } else if (min > 0) {
                sb.append(min + "分钟前");
            } else {
                sb.append(sec + "秒前");
            }

            resultTimes = sb.toString();
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return resultTimes;
    }

//	/**
//     * 根据年和周获取上周周一
//     * @param year
//     * @param weekNo
//     * @return
//     */
//    public static String getStartDayOfWeekNo1(int year,int weekNo){
//        Calendar cal = getCalendarFormYear(year);
//        cal.set(Calendar.WEEK_OF_YEAR, weekNo);
//        cal.add(Calendar.DATE, -6);
//        return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" +
//               cal.get(Calendar.DAY_OF_MONTH);
//
//    }
//
//	 /**
//     * 根据年和周获取周一
//     * @param year
//     * @param weekNo
//     * @return
//     */
//    public static String getStartDayOfWeekNo(int year,int weekNo){
//        Calendar cal = getCalendarFormYear(year);
//        cal.set(Calendar.WEEK_OF_YEAR, weekNo);
//        return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" +
//               cal.get(Calendar.DAY_OF_MONTH);
//
//    }
//
//    /**
//     * 根据年和周获取上周周日
//     * @param year
//     * @param weekNo
//     * @return
//     */
//    public static String getEndDayOfWeekNo1(int year,int weekNo){
//        Calendar cal = getCalendarFormYear(year);
//        cal.set(Calendar.WEEK_OF_YEAR, weekNo);
//        cal.add(Calendar.DAY_OF_WEEK, 6);
//        cal.add(Calendar.DATE, -7);
//        return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" +
//               cal.get(Calendar.DAY_OF_MONTH);
//    }
//
//    /**
//     * 根据年和周获取周日
//     * @param year
//     * @param weekNo
//     * @return
//     */
//    public static String getEndDayOfWeekNo(int year,int weekNo){
//        Calendar cal = getCalendarFormYear(year);
//        cal.set(Calendar.WEEK_OF_YEAR, weekNo);
//        cal.add(Calendar.DAY_OF_WEEK, 6);
//        return cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1) + "-" +
//               cal.get(Calendar.DAY_OF_MONTH);
//    }

    /**
     * 获取一年中第几周
     *
     * @param year
     * @param weekNo
     * @return
     */
    public static int getWeek(String date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(fomatDate(date));
        return cal.get(Calendar.WEEK_OF_YEAR);
    }
    /*/private static Calendar getCalendarFormYear(int year){
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.YEAR, year);
        return cal;
    }

    /**
     * 根据年和月获取月的第一天
     * @param year
     * @param month
     * @return
     *//*
    public static String getFirstDayOfMonth(int year,int month){
        String monthStr = month < 10 ? "0" + month : String.valueOf(month);
        return year + "-"+monthStr+"-" +"01";
    }

    *//**
     * 根据年和月获取月的最后一天
     * @param year
     * @param month
     * @return
     *//*
    public static String getLastDayOfMonth(int year,int month){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR , year);
        calendar.set(Calendar.MONTH , month - 1);
        calendar.set(Calendar.DATE , 1);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_YEAR , -1);
        return calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH) + 1) + "-" +
               calendar.get(Calendar.DAY_OF_MONTH);
    }*/

    /**
     * 当前季度的开始时间
     *
     * @return
     */
    public static String getCurrentQuarterStartTime() {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3)
                c.set(Calendar.MONTH, 0);
            else if (currentMonth >= 4 && currentMonth <= 6)
                c.set(Calendar.MONTH, 3);
            else if (currentMonth >= 7 && currentMonth <= 9)
                c.set(Calendar.MONTH, 4);
            else if (currentMonth >= 10 && currentMonth <= 12)
                c.set(Calendar.MONTH, 9);
            c.set(Calendar.DATE, 1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return longSdf.format(now);
    }

    /**
     * 当前季度的结束时间
     *
     * @return
     */
    public static String getCurrentQuarterEndTime() {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        int currentMonth = c.get(Calendar.MONTH) + 1;
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                c.set(Calendar.MONTH, 2);
                c.set(Calendar.DATE, 31);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                c.set(Calendar.MONTH, 8);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return longSdf.format(now);
    }

    /**
     * @param s
     * @param e
     * @return int
     * @throws
     * @Title: compareDateTime
     * @Description: (日期比较 ， 如果s > e 返回1 ; s = e 返回0 ; s < e 返回 - 1)
     */
    public static int compareDateTime2(String s, String e) throws ParseException {
        SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (longSdf.parse(s) == null) {
            return -1;
        } else if (longSdf.parse(e) == null) {
            return 1;
        } else if (longSdf.parse(s).getTime() > longSdf.parse(e).getTime()) {
            return 1;
        } else if (longSdf.parse(s).getTime() == longSdf.parse(e).getTime()) {
            return 0;
        } else {
            return -1;
        }
    }

    /**
     * 根据格式为yyyy-MM-dd HH:mm:ss的时间格式转换时间戳类型数据
     *
     * @param formatTime
     * @return
     */
    public static Long getStampTimeForString(String formatTime) throws ParseException {
        return sdfTimeForWeb.parse(formatTime).getTime();
    }

    /**
     * 获取三个月前的时间
     *
     * @return
     */
    public static String getThreeMonthsAgo(int count) {
        Date dNow = new Date(); //当前时间
        Date dBefore = new Date();
        Calendar calendar = Calendar.getInstance(); //得到日历
        calendar.setTime(dNow);//把当前时间赋给日历
        calendar.add(Calendar.MONTH, -count); //设置为前3月
        dBefore = calendar.getTime(); //得到前3月的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); //设置时间格式
        String defaultStartDate = sdf.format(dBefore); //格式化前3月的时间
        return defaultStartDate;
    }

    /**
     * 获取当前格式时间
     *
     * @return
     */
    public static Date getSdfTime2(String date) throws ParseException {
        return sdfTime2.parse(date);
    }

    /**
     * 获取当前格式时间
     *
     * @return
     */
    public static String getSdfTimeForWeb(Date date) {
        return sdfTimeForWeb.format(date);
    }

    /**
     * 获取当前格式时间
     *
     * @return
     */
    public static Date getSdfDays(String date) throws ParseException {
        return sdfDays.parse(date);
    }

    /**
     * 获取当前格式时间
     *
     * @return
     */
    public static String getSdfDays2(Date date) {
        return sdfDays2.format(date);
    }

    /**
     * java 获取 获取某年某月 所有日期（yyyy-mm-dd格式字符串）
     *
     * @param year
     * @param month
     * @return
     */
    public static List<String> getMonthFullDay(int year, int month) {
        SimpleDateFormat dateFormatYYYYMMDD = new SimpleDateFormat("yyyy-MM-dd");
        List<String> fullDayList = new ArrayList<>(32);
        // 获得当前日期对象
        Calendar cal = Calendar.getInstance();
        cal.clear();// 清除信息
        cal.set(Calendar.YEAR, year);
        // 1月从0开始
        cal.set(Calendar.MONTH, month - 1);
        // 当月1号
        cal.set(Calendar.DAY_OF_MONTH, 1);
        int count = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int j = 1; j <= count; j++) {
            fullDayList.add(dateFormatYYYYMMDD.format(cal.getTime()));
            cal.add(Calendar.DAY_OF_MONTH, 1);
        }
        return fullDayList;
    }

    /**
     * 判断今天是不是周天
     */
    public static int checkSunday() {
        // 创建Calendar类实例
        Calendar instance = Calendar.getInstance();
        // 获取今天星期几
        int i = instance.get(Calendar.DAY_OF_WEEK);
        // 获取周六日
        int saturday = Calendar.SATURDAY;
        int sunday = Calendar.SUNDAY;
        // 判断今天是否是周六日
        if (i == saturday) {
            return 6;
        } else if (i == sunday) {
            return 7;
        } else {
            return 0;
        }
    }

    /**
     * 判断是否在同一个月
     *
     * @return false:不在同一个月内，true在同一个月内
     */
    public static boolean isMonth(Date date1, Date date2) {
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(date1);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(date2);
        int year1 = calendar1.get(Calendar.YEAR);
        int year2 = calendar2.get(Calendar.YEAR);
        int month1 = calendar1.get(Calendar.MONTH);
        int month2 = calendar2.get(Calendar.MONTH);
        System.out.println(year1 + "  " + month1);
        System.out.println(year2 + "  " + month2);
        return calendar1.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR) && calendar1.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH);

    }

    /**
     * 计算两个日期之间的时间差值
     * @param start
     * @param end
     * @return
     */
    public static String dateDifferenceCalculator(LocalDateTime start, LocalDateTime end) {
        // 计算两个日期之间的Duration
        Duration duration = Duration.between(start, end);
        // 计算天数
        long days = duration.toDays();
        // 计算剩余的小时数
        long hours = duration.toHours() % 24;
        // 计算剩余的分钟数
        long minutes = duration.toMinutes() % 60;
        // 计算剩余的秒数
        long seconds = duration.getSeconds() % 60;
        // 输出结果
        String result = null;
        if (days > 0) {
            result =  hours > 0 ? String.format("%d天%d小时", days, hours) : String.format("%d天", days);
        } else if (hours > 0) {
            result = minutes > 0 ? String.format("%d小时%d分钟", hours, minutes) : String.format("%d小时", hours);
        } else if (minutes > 0) {
            result = String.format("%d分钟", minutes);
        } else if (seconds > 0) {
            result = String.format("%d秒", seconds);
        }
        return result;
    }
    /**
     * 判断一个日期是不是昨天
     * @return
     */
    public static Boolean isAfterDay(String dateString) {
        Date date = fomatDateTime(dateString);

        Calendar canlendar = Calendar.getInstance(); // java.util包
        canlendar.setTime(fomatDateTime(getDay() + " 00:00:00"));
        canlendar.add(Calendar.DATE, -1); // 日期减 如果不够减会将月变动
        Date preDateStart = canlendar.getTime();

        Calendar canlendar1 = Calendar.getInstance(); // java.util包
        canlendar1.setTime(fomatDateTime(getDay() + " 23:59:59"));
        canlendar1.add(Calendar.DATE, -1); // 日期减 如果不够减会将月变动
        Date preDateEnd = canlendar1.getTime();

        if ((date.compareTo(preDateStart) >=0 ) && (date.compareTo(preDateEnd) <=0 )) {
            return true;
        }
        return false;
    }

    public static boolean checkSunDay(String date) {
        String[] dateArr = date.split("-");
        LocalDate today = LocalDate.of(Integer.valueOf(dateArr[0]), Integer.valueOf(dateArr[1]), Integer.valueOf(dateArr[2]));	 // 获取当前日期
        DayOfWeek dayOfWeek = today.getDayOfWeek(); // 获取星期几
        if (dayOfWeek == DayOfWeek.SUNDAY) {
            return true;
        } else {
            return false;
        }
    }

    public static String getSunDay(String date) {
        String[] dateArr = date.split("-");
        LocalDate today = LocalDate.of(Integer.valueOf(dateArr[0]), Integer.valueOf(dateArr[1]), Integer.valueOf(dateArr[2]));	 // 获取当前日期
        LocalDate sunday = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); // 获取本周的周日
        return sunday.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    /**
     * 字符串转日期格式（yyyy/MM/dd）
     *
     * @param date 字符串类型正确日期数据
     * @return 日期
     */
    public static String fomatDateNew(String date) throws ParseException {
        // 创建一个带有时分秒的日期对象
        Date dateNew = sdfTimeForWeb.parse(date);

        // 格式化日期，去掉时分秒
        String dateWithoutTime = sdfDays2.format(dateNew);
        return dateWithoutTime;
    }

    /**
     * 根据格式为yyyy-MM-dd HH:mm:ss的时间格式转换时间戳类型数据
     *
     * @param formatTime
     * @return
     */
    public static Date getStampTimeForDate(String formatTime) throws ParseException {
        return sdfTimeForWeb.parse(formatTime);
    }

}

